//
//  WebviewControl.m
//  UI_Project_A
//
//  Created by GH on 16/2/28.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "WebviewControl.h"

@interface WebviewControl ()<UIWebViewDelegate>

@property (nonatomic,retain)UIActivityIndicatorView *biao;



@end

@implementation WebviewControl

-(void)dealloc{
    [_urlstring release];
    [_biao release];
    [_webview release];
    [super dealloc];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.webview = [[UIWebView alloc]init];
    
    self.webview.frame = CGRectMake(0, 0, 414, 735);
    [self.view addSubview:self.webview];
    self.webview.backgroundColor = [UIColor whiteColor];
    
    self.webview.delegate = self;
    
    
    NSURL* url = [NSURL URLWithString:self.urlstring];//创建URL
    NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
    
    [self.webview loadRequest:request];
    [self.webview release];
    
    
    
    NSLog(@"%@",self.urlstring);
    
    
    UIView  *viewOfrenturn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 70)];
    
    [self.webview addSubview:viewOfrenturn];
    viewOfrenturn.backgroundColor = [UIColor redColor];
    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 10, 50, 50);
    [viewOfrenturn addSubview:button];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];

    
    
    
    
    
   
    

}
- (void)returnClick: (UIButton *)button {

    [self dismissViewControllerAnimated:YES completion:nil];


}




-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    NSLog(@"webDidstarLoad");
    
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,width ,height)];
    
    [view setTag:100];
    
    
    view.backgroundColor = [UIColor blackColor];
    
    view.alpha = 0.4;
    
    [self.webview addSubview:view];
    
    
    view.center = self.webview.center;
    self.biao = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
    
    self.biao.center = self.view.center;
    
    [self.biao setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    [view addSubview:self.biao];
    
    [self.biao startAnimating];
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [self.biao startAnimating];
    
    UIView *view = [self.view viewWithTag:100];
    
    [view removeFromSuperview];
    
    
    
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ISNIGHT"]) {

    
        // font color
        [self.webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#DCDCDC'"];
        //  body backgroud color
        
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('news')[0].style.background='#303030'"];
    
    }else {
        
        [self.webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#696969'"];
        //  body backgroud color
        
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('news')[0].style.background='#fafafa'"];
        
    }

    
    

    NSLog(@"webDidtstarFinish");
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [self.biao startAnimating];
    UIView *view = [self.view viewWithTag:100];
    [view removeFromSuperview];
    NSLog(@"didFailWithError:%@",error);
    
    
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
